package com.irdstudio.tdp.executor.core.dao;

import com.irdstudio.tdp.executor.core.dao.domain.SSrvsCronHis;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SSrvsCronHisDao {

    /* 连接对象 */
    Connection conn = null;

    public SSrvsCronHisDao(Connection conn){
        this.conn = conn;
    }

    /**
     * 查询列表
     * @param
     * @return
     * @throws SQLException
     */
    public List<SSrvsCronHis> queryAll()throws SQLException {
        List<SSrvsCronHis> sSrvsCronHisList = new ArrayList<SSrvsCronHis>();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("SELECT * FROM s_srvs_cron_his");
            rs = ps.executeQuery();
            while (rs.next()) {
                SSrvsCronHis sSrvsCronHis = new SSrvsCronHis();
                sSrvsCronHis.setRecordId(rs.getString("record_id"));
                sSrvsCronHis.setJobDesc(rs.getString("job_desc"));
                sSrvsCronHis.setState(rs.getString("state"));
                sSrvsCronHis.setAgentId(rs.getString("agent_id"));
                sSrvsCronHis.setCostTime(rs.getBigDecimal("cost_time"));
                sSrvsCronHis.setEndTime(rs.getString("end_time"));
                sSrvsCronHis.setStartTime(rs.getString("start_time"));
                sSrvsCronHis.setResultDesc(rs.getString("result_desc"));
                sSrvsCronHis.setJobCode(rs.getString("job_code"));
                sSrvsCronHisList.add(sSrvsCronHis);
            }
        } catch (SQLException e) {
            throw new SQLException("queryAll is Wrong!"+ e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return sSrvsCronHisList;
    }

    /**
     * 根据主键查询单条记录
     * @param
     * @return
     * @throws SQLException
     */
    public SSrvsCronHis queryByPK(String recordId) throws SQLException {
        SSrvsCronHis sSrvsCronHis = new SSrvsCronHis();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("SELECT * FROM s_srvs_cron_his where record_id = ?");
            ps.setString(1, recordId);
            rs = ps.executeQuery();
            while (rs.next()) {
                sSrvsCronHis.setRecordId(rs.getString("record_id"));
                sSrvsCronHis.setJobDesc(rs.getString("job_desc"));
                sSrvsCronHis.setState(rs.getString("state"));
                sSrvsCronHis.setAgentId(rs.getString("agent_id"));
                sSrvsCronHis.setCostTime(rs.getBigDecimal("cost_time"));
                sSrvsCronHis.setEndTime(rs.getString("end_time"));
                sSrvsCronHis.setStartTime(rs.getString("start_time"));
                sSrvsCronHis.setResultDesc(rs.getString("result_desc"));
                sSrvsCronHis.setJobCode(rs.getString("job_code"));
            }
        } catch (SQLException e) {
            throw new SQLException("queryByPK is Wrong!"+ e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return sSrvsCronHis;
    }

    /**
     * 根据定时作业编号查询单条记录
     * @param
     * @return
     * @throws SQLException
     */
    public List<SSrvsCronHis> queryByJobCode(String jobCode) throws SQLException {
        List<SSrvsCronHis> sSrvsCronHisList = new ArrayList<SSrvsCronHis>();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("SELECT * FROM s_srvs_cron_his where job_code = ?");
            ps.setString(1, jobCode);
            rs = ps.executeQuery();
            while (rs.next()) {
                SSrvsCronHis sSrvsCronHis = new SSrvsCronHis();
                sSrvsCronHis.setRecordId(rs.getString("record_id"));
                sSrvsCronHis.setJobDesc(rs.getString("job_desc"));
                sSrvsCronHis.setState(rs.getString("state"));
                sSrvsCronHis.setAgentId(rs.getString("agent_id"));
                sSrvsCronHis.setCostTime(rs.getBigDecimal("cost_time"));
                sSrvsCronHis.setEndTime(rs.getString("end_time"));
                sSrvsCronHis.setStartTime(rs.getString("start_time"));
                sSrvsCronHis.setResultDesc(rs.getString("result_desc"));
                sSrvsCronHis.setJobCode(rs.getString("job_code"));
                sSrvsCronHisList.add(sSrvsCronHis);
            }
        } catch (SQLException e) {
            throw new SQLException("queryByAgentId is Wrong!"+ e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return sSrvsCronHisList;
    }

    /**
     * 根据主键更新
     * @param
     * @return
     * @throws SQLException
     */
    public int updateByPk(SSrvsCronHis sSrvsCronHis){
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("UPDATE s_srvs_cron_his SET"
                    + " record_id = ? ,"
                    + " job_code = ? ,"
                    + " job_desc = ? ,"
                    + " state = ? ,"
                    + " start_time = ? ,"
                    + " end_time = ? ,"
                    + " cost_time = ? ,"
                    + " agent_id = ? ,"
                    + " result_desc = ? "
                    +"WHERE record_id=?");
            ps.setObject(1, sSrvsCronHis.getRecordId());
            ps.setObject(2, sSrvsCronHis.getJobCode());
            ps.setObject(3, sSrvsCronHis.getJobDesc());
            ps.setObject(4, sSrvsCronHis.getState());
            ps.setObject(5, sSrvsCronHis.getStartTime());
            ps.setObject(6, sSrvsCronHis.getEndTime());
            ps.setObject(7, sSrvsCronHis.getCostTime());
            ps.setObject(8, sSrvsCronHis.getAgentId());
            ps.setObject(9, sSrvsCronHis.getResultDesc());
            ps.setObject(10, sSrvsCronHis.getRecordId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.getStackTrace();
        } finally {
            close(rs, null, ps);
        }
        return 0;
    }

    /**
     * 新增
     * @param sSrvsCronHis
     * @return
     * @throws SQLException
     */
    public int insertSSrvsCronHis(SSrvsCronHis sSrvsCronHis) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into s_srvs_cron_his (" +
                    "record_id,job_code,job_desc,state,start_time,end_time,cost_time,agent_id,result_desc" +
                    ") values (?,?,?,?,?,?,?,?,?)");
            ps.setObject(1, sSrvsCronHis.getRecordId());
            ps.setObject(2, sSrvsCronHis.getJobCode());
            ps.setObject(3, sSrvsCronHis.getJobDesc());
            ps.setObject(4, sSrvsCronHis.getState());
            ps.setObject(5, sSrvsCronHis.getStartTime());
            ps.setObject(6, sSrvsCronHis.getEndTime());
            ps.setObject(7, sSrvsCronHis.getCostTime());
            ps.setObject(8, sSrvsCronHis.getAgentId());
            ps.setObject(9, sSrvsCronHis.getResultDesc());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert SSrvsCronHis is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
    }

    /**
     * 根据主键删除一行记录
     * @return int
     */
    public int deleteByPk(SSrvsCronHis sSrvsCronHis) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from s_srvs_cron_his where 1=1"
                    + " AND record_id = ? "
            );
            ps.setObject(1, sSrvsCronHis.getRecordId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete SSrvsCronHis is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
    }

    /**
     * 关闭资源
     * @param theRs
     * @param theStmt
     * @param thePs
     */
    protected void close(ResultSet theRs, Statement theStmt,PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
